package com.xckj.teacher.view;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.util.Log;
import android.webkit.WebResourceRequest;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.just.agentweb.AgentWeb;
import com.just.agentweb.WebViewClient;
import com.xckj.teacher.event.OpenBottomTabEvent;

import org.greenrobot.eventbus.EventBus;

/**
 * @Author: ChenYao
 * @Describe: 自定义WebView
 * @Date: 2024/6/7 20:37
 */
public class CustomWebView extends FrameLayout {
    private AgentWeb mAgentWeb;
    private Activity mActivity;

    public CustomWebView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public CustomWebView(@NonNull Context context) {
        super(context);
        init(context);
    }

    @SuppressLint("SetJavaScriptEnabled")
    private void init(Context context) {
        mActivity = (Activity) context;
        mAgentWeb = AgentWeb.with((Activity) context).setAgentWebParent(this, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)).closeIndicator().interceptUnkownUrl().closeWebViewClientHelper().setWebViewClient(mWebViewClient).createAgentWeb().ready().get();
        mAgentWeb.getAgentWebSettings().getWebSettings().setUseWideViewPort(true);
        mAgentWeb.getAgentWebSettings().getWebSettings().setLoadWithOverviewMode(true);
        mAgentWeb.getAgentWebSettings().getWebSettings().setJavaScriptEnabled(true);
        mAgentWeb.getAgentWebSettings().getWebSettings().setBuiltInZoomControls(true);
        mAgentWeb.getAgentWebSettings().getWebSettings().setAllowFileAccessFromFileURLs(true);
        mAgentWeb.getAgentWebSettings().getWebSettings().setAllowUniversalAccessFromFileURLs(true);
        mAgentWeb.getAgentWebSettings().getWebSettings().setDomStorageEnabled(true);
        mAgentWeb.getAgentWebSettings().getWebSettings().setDisplayZoomControls(false);
    }

    public WebViewClient mWebViewClient = new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(android.webkit.WebView view, WebResourceRequest request) {
            if (request.getUrl().toString().startsWith("learnteacher://goback")) {
                mActivity.finish();
                return true;
            } else if (request.getUrl().toString().startsWith("learnteacher://opentabbar")) {
                EventBus.getDefault().post(new OpenBottomTabEvent());
                mActivity.finish();
                return true;
            }
            return false;
        }

        @Override
        public void onPageStarted(android.webkit.WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onPageFinished(android.webkit.WebView view, String url) {
            super.onPageFinished(view, url);
        }
    };

    public void loadUrl(String url) {
        mAgentWeb.getUrlLoader().loadUrl(url);
    }

    public boolean goBack() {
        return mAgentWeb.back();
    }
}
